package com.pinxixi.content.service;

import com.pinxixi.content.mapper.ContentMapper;
import com.pinxixi.framework.domain.content.Content;
import com.pinxixi.framework.model.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ContentService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ContentMapper contentMapper;

    public ListResponseResult<Content> findAll() {
        List<Content> list = contentMapper.findAll();
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        return new ListResponseResult<Content>(CommonCode.SUCCESS,queryResult);
    }

    public ResponseResult save(Content content) {
        if(content.getId()==null){
            contentMapper.add(content);
        }else{
            contentMapper.update(content);
        }
        //更新Redis
        this.updateContentCache(content.getCategoryId());
        return ResponseResult.SUCCESS();
    }

    public QueryResponseResult findOne(Long id) {
        Content content = contentMapper.findOne(id);
        return new QueryResponseResult(CommonCode.SUCCESS,content);
    }

    public ResponseResult delete(Long id) {
        Content content = contentMapper.findOne(id);
        contentMapper.delete(id);
        //更新Redis
        this.updateContentCache(content.getCategoryId());
        return ResponseResult.SUCCESS();
    }

    private void updateContentCache(Long categoryId){
        List<Content> list = contentMapper.findContentByCategoryId(categoryId);
        Map contentMap = redisTemplate.opsForHash().entries("content");
        if(categoryId==1L){
            contentMap.put("portal",list);
        }else if(categoryId==2L){
            contentMap.put("list",list);
        }
        redisTemplate.opsForHash().putAll("content",contentMap);
    }

    public ListResponseResult<Content> portalContent() {
        //Map user10001 = new HashMap();
        //user10001.put("sername","zhangsan")
        //user10001.put("age","20")
        //user10001.put("sex","0")


        //hashKey        key        value
        //user10001   username   zhangsan
        //                age         20
        //                sex         0

        //content       portal      List
        //              like        List



        Map contentMap = redisTemplate.opsForHash().entries("content");
        List<Content> list = null;
        if(!contentMap.isEmpty() && contentMap.get("portal")!=null){
            //redis中有首页广告的缓存数据
            list = (List<Content>) contentMap.get("portal");
        }else{
            //redis中没有数据
            list = contentMapper.findContentByCategoryId(1L);
            contentMap.put("portal",list);
            redisTemplate.opsForHash().putAll("content",contentMap);
        }

        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        return new ListResponseResult<Content>(CommonCode.SUCCESS,queryResult);
    }
}
